package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Tag(name = "轮播图管理")
@RestController
@RequestMapping("/api/banners")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary ="查询所有轮播图")
    @GetMapping("/list")
    public Result<List<Banner>> list() {
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按照排序字段排序
        bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        // 查询所有轮播图
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list);
    }


    @Operation(summary ="查询激活状态的轮播图")
    @GetMapping("/active")
    public Result<List<Banner>> active() {
        List<Banner> list = bannerService.list(new LambdaQueryWrapper<Banner>().eq(Banner::getIsActive, true));
        return Result.success(list);
    }

    @Operation(summary ="根据id查询轮播图")
    @GetMapping("/{id}")
    public Result<Banner> getById(@PathVariable Long id) {
        Banner banner = bannerService.getById(id);
        return Result.success(banner);
    }

//    @Operation(summary ="添加轮播图信息")
//    @PostMapping("/add")
//    public Result<String> add(@RequestBody Banner banner) {
//        boolean save = bannerService.save(banner);
//        if (save) {
//            return Result.success("添加成功");
//        }
//        return Result.error("添加失败");
//    }
//
//    @Operation(summary ="更新轮播图信息")
//    @PutMapping("/update")
//    public Result<String> update(@RequestBody Banner banner) {
//        boolean update = bannerService.updateById(banner);
//        if (update) {
//            return Result.success("更新成功");
//        }else {
//            return Result.error("更新失败");
//        }
//    }

    @Operation(summary ="根据id删除轮播图信息")
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        boolean delete = bannerService.removeById(id);
        if (delete) {
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @Operation(summary ="添加或更新轮播图")
    @RequestMapping(value={"/add", "/update"}, method = {RequestMethod.POST, RequestMethod.PUT})
    public Result<String> saveOrUpdate(@RequestBody Banner banner) {
        boolean b= bannerService.saveOrUpdate(banner);
        if (b) {
            return Result.success("添加或更新成功");
        }else {
            return Result.error("添加或更新失败");
        }
    }

    @Operation(summary ="上传轮播图")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file){
        //调用FileUploadService中的uploadFile方法
        String url = fileUploadService.uploadFile(file);
        return Result.success(url,"上传轮播图成功");
    }


    @Operation(summary ="更新轮播图激活状态")
    @PutMapping("/toggle/{id}")
    public Result toggle(@PathVariable Long id, @RequestParam Boolean isActive) {
        //创建LambdaUpdateWrapper对象
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //设置更新字段
        bannerLambdaUpdateWrapper.set(Banner::getIsActive,isActive).set(Banner::getUpdateTime, new Date());
        //设置更新条件
        bannerLambdaUpdateWrapper.eq(Banner::getId, id);
        //更新
        boolean update = bannerService.update(bannerLambdaUpdateWrapper);
        if (update) {
            return Result.success("更新激活状态成功");
        }else{
            return Result.error("更新激活状态失败");
        }
    }
}
